package com.xcdh.commons.spring.aspect;


import com.xcdh.commons.annotation.XCDHAspect;
import com.xcdh.commons.spring.SpringUtils;
import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class XCDHAspectHandler {
    @Pointcut("@annotation(com.xcdh.commons.annotation.XCDHAspect) || within(@com.xcdh.commons.annotation.XCDHAspect *) ")
    public void XCDHAspect() {}


    @SneakyThrows
    @Around("XCDHAspect() && @annotation(xcdhAspect)")
    public Object handlerAfter(ProceedingJoinPoint joinPoint, XCDHAspect xcdhAspect) {
        XCDHAspectInter aspectInter = newXCDHAspectInter(xcdhAspect);
        aspectInter.before(joinPoint);
        Object proceed = aspectInter.round(joinPoint);
        aspectInter.after(joinPoint);
        return proceed;
    }

    private XCDHAspectInter newXCDHAspectInter(XCDHAspect xcdhAspect ){
        return SpringUtils.getBean(xcdhAspect.value()) ;
    }
}
